package org.shoukaiseki.inittable.handle;

import org.shoukaiseki.common.utils.ListUtils;
import org.shoukaiseki.inittable.annotation.TableBuildScan;
import org.shoukaiseki.inittable.logger.InitTableLoggers;
import org.slf4j.Logger;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.List;
import java.util.Set;

public  class TableBuildPackageScanRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {


    public static final Logger logger = InitTableLoggers.DEFAULT_LOGGER;


    private static List<BeanDefinitionHolder> beanList = ListUtils.newArrayList();



    ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {

        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(TableBuildScan.class.getName()));
        String[] basePackages = annoAttrs.getStringArray("basePackage");
        //自定义的 包扫描器
        TableBuildPackageScanHandle scanHandle = new TableBuildPackageScanHandle(beanDefinitionRegistry,false);
        //扫描指定路径下的接口
        Set<BeanDefinitionHolder> beanDefinitionHolders = scanHandle.doScan(basePackages);
        beanList.addAll(beanDefinitionHolders);
    }

    public static List<BeanDefinitionHolder> getBeanList() {
        return beanList;
    }

}
